package org.finesys.common.redisson.config;

import cn.hutool.core.util.StrUtil;
import lombok.RequiredArgsConstructor;
import org.finesys.common.redisson.constants.RedisConstant;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.ClusterServersConfig;
import org.redisson.config.Config;
import org.redisson.config.SentinelServersConfig;
import org.redisson.config.SingleServerConfig;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.util.StringUtils;

import java.util.Objects;

@AutoConfiguration
@AutoConfigureAfter(RedisAutoConfiguration.class)
@RequiredArgsConstructor
public class RedissonClientAutoConfiguration {

    private final RedisProperties redisProperties;

    @Bean
    public RedissonClient redissonClient() {
        Config config = new Config();
        //哨兵模式
        if (Objects.nonNull(redisProperties.getSentinel())) {
            //配置哨兵
            initSentinelConfig(config);
        } else if (Objects.nonNull(redisProperties.getCluster())) {
            //集群模式
            initClusterConfig(config);
        } else {
            //单机模式
            initSingleConfig(config);
        }
        return Redisson.create(config);
    }

    //哨兵
    private void initSentinelConfig(Config config) {

        SentinelServersConfig sentinelServersConfig = config.useSentinelServers();
        //主节点
        sentinelServersConfig.setMasterName(redisProperties.getSentinel().getMaster());
        sentinelServersConfig.addSentinelAddress(redisProperties.getSentinel().getNodes().toArray(new String[0]));
        //密码
        if (StringUtils.hasText(redisProperties.getPassword())) {
            sentinelServersConfig.setPassword(redisProperties.getPassword());
        }
    }

    //集群
    private void initClusterConfig(Config config) {
        ClusterServersConfig clusterServersConfig = config.useClusterServers();
        //集群节点地址
        clusterServersConfig.addNodeAddress(redisProperties.getCluster().getNodes().stream().map(node -> RedisConstant.REDIS_PREFIX + node).toArray(String[]::new));
        //密码
        if (StringUtils.hasText(redisProperties.getPassword())) {
            clusterServersConfig.setPassword(redisProperties.getPassword());
        }
    }

    //单机模式
    private void initSingleConfig(Config config) {
        SingleServerConfig singleServerConfig = config.useSingleServer();
        //节点地址
        String address = RedisConstant.REDIS_PREFIX + redisProperties.getHost() + StrUtil.COLON + redisProperties.getPort();
        singleServerConfig.setAddress(address);
        singleServerConfig.setDatabase(redisProperties.getDatabase());
        //密码
        if (StringUtils.hasText(redisProperties.getPassword())) {
            singleServerConfig.setPassword(redisProperties.getPassword());
        }
    }


}
